//
//  Extension.swift
//  TestDemo
//
//  Created by 赵洋 on 2017/5/31.
//  Copyright © 2017年 赵洋. All rights reserved.
//

import UIKit
import MBProgressHUD

let screenWidth = UIScreen.main.bounds.size.width
let screenHeight = UIScreen.main.bounds.size.height
let screenRect = UIScreen.main.bounds

extension UIView {
    var x:CGFloat{
        get{
            return frame.origin.x
        }
        set(newX){
            frame.origin.x = newX
        }
    }
    
    var y:CGFloat{
        get{
            return frame.origin.y
        }
        set(newY){
            frame.origin.y = newY
        }
    }
    
    var centerX:CGFloat{
        get{
            return center.x
        }
        set(newCenterX){
            center.x = newCenterX
        }
    }
    
    var centerY:CGFloat{
        get{
            return center.y
        }
        set(newCenterY){
            center.y = newCenterY
        }
    }
    
    var width:CGFloat{
        get{
            return frame.size.width
        }
        set(newWidth){
            frame.size.width = newWidth
        }
    }
    
    var height:CGFloat{
        get{
            return frame.size.height
        }
        set(newHeight){
            frame.size.height = newHeight
        }
    }
    var origin:CGPoint {
        get{
            return frame.origin
        }
        set(newOrigin){
            frame.origin = newOrigin
        }
    }
    
    var size:CGSize{
        get{
            return frame.size
        }
        set(newSize){
            frame.size = newSize
        }
    }
}
extension String{
    func URLEncode() -> String{
        return self.addingPercentEncoding(withAllowedCharacters:.urlHostAllowed)!
    }
}


extension MBProgressHUD{
    
    static let globalHud:MBProgressHUD = {
        let instance = MBProgressHUD()
        return instance
    }()
    
    static func showLoading(){
        MBProgressHUD.showLoadingWith(Text: nil)
    }
    static func showLoadingWith(Text text:String?){
        globalHud.removeFromSuperViewOnHide  = true
        UIApplication.shared.windows.last!.addSubview(globalHud)
        if let text = text {
            globalHud.label.text = text
        }
        globalHud.frame = screenRect
        globalHud.show(animated: true)
    }
    static func hideLoading(){
        globalHud.hide(animated: true)
    }
    static func showSuccessWith(text:String){
        MBProgressHUD.showSuccesssWith(text: text, toView: nil)
    }
    static func showSuccesssWith(text:String, toView view:UIView?){
        MBProgressHUD .showTextAndIconWith(text: text, icon: "Checkmark", toView: view)
    }
    static func showErrorWith(text:String){
        MBProgressHUD.showErrorWith(text: text, toView: nil)
    }
    static func showErrorWith(text:String, toView view:UIView?){
        MBProgressHUD .showTextAndIconWith(text: text, icon: "cancel", toView: view)
    }
    static func showTextAndIconWith(text:String, icon:String, toView view:UIView?){
        var view = view
        if view == nil {
            view = UIApplication.shared.windows.last!
        }
        let hud = MBProgressHUD.showAdded(to: view!, animated: true)
        hud.label.text = text
        hud.customView = UIImageView(image: UIImage(named: icon))
        hud.mode = .customView
      
        hud.removeFromSuperViewOnHide = true
        hud.hide(animated: true, afterDelay: 2)
    }

}

extension CGRect {
    var x:CGFloat {
        get {
            return origin.x
        }
        set(newX){
            origin.x = newX
        }
    }
    var y:CGFloat {
        get {
            return origin.y
        }
        set(newY){
            origin.y = newY
        }
    }
    var width:CGFloat {
        get {
            return size.width
        }
        set(newWidth){
            size.width = newWidth
        }
    }
    var height:CGFloat {
        get {
            return size.height
        }
        set(newHeight){
            size.height = newHeight
        }
    }
   

}
